package com.dhnsoft.代码随想录.栈与队列;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * ClassName: d04_1047_删除字符串中的所有相连重复项
 * Package: com.dhnsoft.代码随想录.栈与队列
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/26 10:28
 * @Version 1.0
 */
public class d04_1047_删除字符串中的所有相连重复项 {

    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        char[] arr = s.toCharArray();
        for (char c : arr) {
            if(!stack.isEmpty()){
                if(c == stack.peek()){
                    stack.pop();
                    continue;
                }
            }
            stack.push(c);
        }
        Stack<Character> newStack = new Stack<>();
        while (!stack.isEmpty()){
            newStack.push(stack.pop());
        }
        StringBuilder stringBuilder = new StringBuilder();
        while (!newStack.isEmpty()){
            stringBuilder.append(newStack.pop());
        }
        return stringBuilder.toString();
    }
}
